﻿using Bentley.CifNET.GeometryModel.SDK.Edit;
using Bentley.GeometryNET;
using System.Collections.Generic;
using Bentley.DgnPlatformNET;
using Bentley.CifNET.LinearGeometry;
using System.Windows.Forms;
using System;
using BIM = Bentley.Interop.MicroStationDGN;
using BMI = Bentley.MstnPlatformNET.InteropServices;
using Bentley.DgnPlatformNET.Elements;
using System.Diagnostics;
using Bentley.CifNET.SDK;
using Bentley.CifNET.SDK.Edit;
using Bentley.CifNET.GeometryModel.SDK;
using BCL = Bentley.CifNET.LinearGeometry;
using System.IO;
using System.Linq;
using Bentley.MstnPlatformNET;
using Bentley.CifNET.Geometry;
using System.Runtime.InteropServices;
using System.Drawing;

/*
 * 构件多面mesh, 适用于不规则多面体
 * 注意:顶部点与底部点必须相同, 索引号相同的连线构成竖向边,点的顺序为从顶视图看顺时针
 * 调用举例:
 *          List<DPoint3d> pts = new List<DPoint3d>()
            {
                new DPoint3d(1,1,2),
                new DPoint3d(1, 0, 2),
                new DPoint3d(0.5, 0, 2),
            };    
            List<DPoint3d> pts2=new List<DPoint3d>()
            {
                new DPoint3d(1,1,0),
                new DPoint3d(1, 0, 0),
                new DPoint3d(0.5, 0, 0),
            };
            Element ele =CheccPolyFaceSolid.Create(pts, pts2, "八字墙", SolidColor.青色, SolidLineStyle.短虚线, 0);
            if (ele != null) ele.AddToModel();
*/

namespace CheccCoverplateCulvertAssemble.Operation.HXD
{
    internal class CheccPolyFaceSolid
    {
        private static double UOR_TO_MASTER =1.0 / SolidHelper.UOR;
        //顶部点与底部点 索引号相同的连线构成边 单位米
        public static MeshHeaderElement Create(List<DPoint3d> PtTops, List<DPoint3d> PtBottoms, DgnModel dgnModel, string levelName, SolidColor solidColor, SolidLineStyle solidLineStyle, int lineWidth)
        {
            if (PtTops.Count != PtBottoms.Count) return null;
            PolyfaceHeader polyHeader = new PolyfaceHeader();
            polyHeader.NumberPerFace = 0;
            //添加所有的顶点
            var pts_top = new List<DPoint3d>();
            for (int i = 0; i < PtTops.Count; i++)
            {
                pts_top.Add(SolidHelper.MeterToUor(PtTops[i]));
            }
            var pts_bottom = new List<DPoint3d>();
            for (int i = 0; i < PtBottoms.Count; i++)
            {
                pts_bottom.Add(SolidHelper.MeterToUor(PtBottoms[i]));
            }


            //构建顶面
            PolyfaceHeader polyHeader_top = new PolyfaceHeader();
            polyHeader_top.Point = pts_top;
            var indices = new List<int>();
            for (int i = pts_top.Count-1; i >= 0; i--)
            {
                indices.Add(i+1);
            }
            polyHeader_top.AddIndexedFacet(indices, null, null, indices);
            indices.Clear();

            //构建底面
            PolyfaceHeader polyHeader_bottom = new PolyfaceHeader();
            polyHeader_bottom.Point = pts_bottom;
            indices = new List<int>();
            for (int i = 0; i < pts_bottom.Count; i++)
            {
                indices.Add(i+1);
            }
            polyHeader_bottom.AddIndexedFacet(indices, null, null, indices);
            indices.Clear();

            //构建侧面
            List<PolyfaceHeader> lstPolyHeader_side = new List<PolyfaceHeader>();
            for (int i = 0; i < pts_top.Count-1; i++)
            {
                var pts =new  List<DPoint3d>();
                PolyfaceHeader polyHeader_temp = new PolyfaceHeader();
                pts.Add(pts_top[i]); pts.Add(pts_top[i+1]); pts.Add(pts_bottom[i + 1]); pts.Add(pts_bottom[i]); 
                polyHeader_temp.Point = pts;
                var indicesSide = new List<int>();
                indicesSide.Add(1); indicesSide.Add(2); indicesSide.Add(3); indicesSide.Add(4);
                polyHeader_temp.AddIndexedFacet(indicesSide, null, null, indicesSide);
                indicesSide.Clear();
                lstPolyHeader_side.Add(polyHeader_temp);    
            }

            //构建侧面 尾_首
            var pts1 = new List<DPoint3d>();
            PolyfaceHeader polyHeader_temp1 = new PolyfaceHeader();
            pts1.Add(pts_top[pts_top.Count-1]); pts1.Add(pts_top[0]); pts1.Add(pts_bottom[0]); pts1.Add(pts_bottom[pts_bottom.Count-1]);
            polyHeader_temp1.Point = pts1;
            indices = new List<int>();
            indices.Add(1); indices.Add(2); indices.Add(3); indices.Add(4);
            polyHeader_temp1.AddIndexedFacet(indices, null, null, indices);
            indices.Clear();
            lstPolyHeader_side.Add(polyHeader_temp1);

            PolyfaceConstruction polyCon = new PolyfaceConstruction();
            polyCon.Add(polyHeader_top);
            polyCon.Add(polyHeader_bottom);
            for (int i = 0; i < lstPolyHeader_side.Count; i++)
            {
                polyCon.Add(lstPolyHeader_side[i]);
            }
            polyHeader = polyCon.GetClientMesh();

            MeshHeaderElement meshElem = new MeshHeaderElement(dgnModel, null, polyHeader);
            SolidHelper.setElementByLevel(levelName, (int)solidColor, (int)solidLineStyle, lineWidth, meshElem);
            //meshElem.AddToModel();
            return meshElem;
        }
 
    }
}
